package org.lql.algo.codecrush.week007;

/**
 * @author: liangqinglong
 * @date: 2025-08-18 07:54
 * @description: 744. 寻找比目标字母大的最小字母 <a href="https://leetcode.cn/problems/find-smallest-letter-greater-than-target/description/">...</a>
 **/
public class NextGreatestLetter {

	public char nextGreatestLetter(char[] letters, char target) {
		int n = letters.length;
		if (target >= letters[n - 1]) {
			return letters[0];
		}
		int left = 0, right = n - 1;
		while (left < right) {
			int mid = left + (right - left) / 2;
			if (letters[mid] <= target) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}
		return letters[right];
	}

	public static void main(String[] args) {
		NextGreatestLetter n = new NextGreatestLetter();
		char[] letters = new char[]{'c', 'f', 'j'};
		char target = 'a';
		char res = n.nextGreatestLetter(letters, target);
		System.out.println(res);
		target = 'c';
		res = n.nextGreatestLetter(letters, target);
		System.out.println(res);
		letters = new char[]{'x', 'x', 'y', 'y'};
		target = 'z';
		res = n.nextGreatestLetter(letters, target);
		System.out.println(res);
	}
}
